﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;
using BusinessObjects.BO_Actions.Ads_Picture;
using Entity.StoreProcedure;
using System.Collections.Generic;
using System.IO;

public partial class controls_admin_Advertising : Pages_Base
{
    protected int mid;
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = getRequest("mid", -1);
        base.removeHGC(divMessage);
        base.removeHGC(divResult);
        if (!IsPostBack)
        {
            base.loadModuleByType(ddlModule, 2);
            base.loadModuleByType(ddlModule, 5);
            base.loadModuleByType(ddlModule, 6);
            if (ddlModule.Items.Count == 0)
                ddlModule.Items.Add(new ListItem("Chưa có module loại quảng cáo nào", "-1"));
            if (mid != -1)
                ddlModule.SelectedValue = mid.ToString();
            else
                mid = Convert.ToInt32(ddlModule.SelectedValue);
            loadAdv();
        }
    }
    private void loadAdv()
    {
        try
        {
            objDT = BO_Ads_Picture.SelectByModule(mid);
            base.bindRepeater(rptAds, objDT, divMessage);
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message + ex.Source);
        }
        finally { base.CloseConnection(); }
    }
    protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        base.ddlSelectedIndexChanged("mid", ddlModule);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        string del_list = hdfCheck.Value;
        string[] sp_list = del_list.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        BOAds_Picture_Delete _action = new BOAds_Picture_Delete();
        usp_Ads_Picture_Delete _entity;
        List<usp_Ads_Picture_Delete> _listData = new List<usp_Ads_Picture_Delete>();
        foreach (string per in sp_list)
        {
            _entity = new usp_Ads_Picture_Delete();
            _entity.PICTURE_ID = Convert.ToInt32(per);
            _listData.Add(_entity);
        }
        if (_action.ExecuteListDataAndReturnBooleanValue(_listData))
        {
            base.writeAds(mid);
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công.");
        }
        else
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
        loadAdv();
    }
    protected string GetAds(string ads, string title)
    {
        if (ads != "")
        {
            string strReturn = "";
            string extension = Path.GetExtension(ads);
            if (extension == ".swf")
                strReturn = "<object height=' 100' border='0' width='200' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"../upload/ads/" + ads + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"../upload/ads/" + ads + "\"></object>";
            else strReturn = "<img height='100' src='../upload/ads/" + ads + "' alt='" + title + "' onerror='ImgError(this);' />";
            return strReturn;
        }
        else
            return "";

    }
}
